package mirrormonkey.rpc.module;

import mirrormonkey.rpc.messages.RpcErrorMessage;

import com.jme3.network.Message;
import com.jme3.network.MessageConnection;
import com.jme3.network.MessageListener;

public class RpcErrorListener implements MessageListener<MessageConnection> {

	public final CallRegistry callRegistry;

	public RpcErrorListener(CallRegistry callRegistry) {
		this.callRegistry = callRegistry;
	}

	@Override
	public void messageReceived(MessageConnection source, Message message) {
		RpcErrorMessage castMessage = (RpcErrorMessage) message;
		RegisteredCall call = callRegistry
				.getRegisteredCall(castMessage.callId);
		// Can be null due to timeout
		if (call != null) {
			call.errorArrived(source, castMessage);
		}
	}

}
